package com.maigao.persistent;

import com.maigao.entity.jpa.ClassRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface ClassRecordRepository extends JpaRepository<ClassRecord, Long> {
    ClassRecord findById(int id);

    List<ClassRecord> findByIdIn(List<Integer> ids);

    List<ClassRecord> findByStudentId(Integer studentId);

    List<ClassRecord> findByCoachId(Integer coachId);

    List<ClassRecord> findByCoachIdAndStudentId(Integer coachId, Integer studentId);

    @Modifying
    @Query("update ClassRecord set status = ?2 where id = ?1")
    ClassRecord updateStatus(Integer id, Integer status);

    @Query("select cr from ClassRecord cr where cr.coachId = ?1 and ((cr.classFromTime > ?2 and cr.classFromTime < ?3) or (cr.classEndTime > ?4 and cr.classEndTime < ?5)) and cr.status = ?6")
    List<ClassRecord> findClassRecordByCoachAndTime(Integer coachId, LocalDateTime fromTimeStart, LocalDateTime fromTimeEnd,
                                                    LocalDateTime endTimeStart, LocalDateTime endTimeEnd, Integer status);

    List<ClassRecord> findByCoachIdAndStatusAndCreateTimeBetween(Integer coachId, Integer status, LocalDateTime fromTime, LocalDateTime endTime);

    List<ClassRecord> findByCoachIdAndStatusInAndCreateTimeBetween(Integer coachId, List<Integer> status, LocalDateTime fromTime, LocalDateTime endTime);

    long countByCoachIdAndStatus(Integer coachId, Integer status);

    List<ClassRecord> findByCoachIdAndClassFromTimeBetweenAndStatusInOrderByClassFromTime(Integer coachId, LocalDateTime fromTime, LocalDateTime endTime, List<Integer> statusList);

    List<ClassRecord> findByStudentIdInAndCreateTimeBetweenOrderByClassFromTimeDesc(List<Integer> studentIds, LocalDateTime fromTime, LocalDateTime endTime);

    List<ClassRecord> findByStudentIdAndStatusIn(Integer studentId, List<Integer> status);

    List<ClassRecord> findByStudentIdAndStatusInAndStudentCourseId(Integer studentId, List<Integer> status, Integer studentCourseId);

    List<ClassRecord> findByStatusAndClassFromTimeBetween(Integer status, LocalDateTime fromTime, LocalDateTime endTime);
}
